
import store from '../store';
import { createRouter, createWebHistory } from 'vue-router'
import { Notify } from "vant";

const routes = [
  {
    path: '',
    name: 'default',
    component: () => import("../views/home/HomeView.vue"),
    meta: {
      title: '图书兄弟-首页',
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import("../views/home/HomeView.vue"),
    meta: {
      title: '图书兄弟-首页',
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import("../views/category/CategoryView.vue"),
    meta: {
      title: '图书兄弟-商品详情',
    }
  },
  {
    path: '/shopcart',
    name: 'Shopcart',
    component: () => import("../views/shopcart/ShopcartView.vue"),
    meta: {
      title: '图书兄弟-购物车',
      isAuthRequired: true
    }
  },

  {
    path: '/proilte',
    name: 'Proilte',
    component: () => import("../views/proilte/ProliteView.vue"),
    meta: {
      title: '图书兄弟-个人中心',
      isAuthRequired: true
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import("../views/Detail/DetailView.vue"),
    meta: {
      title: '图书兄弟-商品详情',
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import("../views/proilte/register/RegisterView.vue"),
    meta: {
      title: '图书兄弟-用户注册',
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("../views/proilte/login/LoginView.vue"),
    meta: {
      title: '图书兄弟-用户登录',
    }
  },
  {
    //地址管理
    path: '/address',
    name: 'address',
    component: () => import("../views/proilte/addre/AddressView.vue"),
    meta: {
      title: '地址管理'
    }
  },
  {

    //地址编辑
    path: '/addresslist',
    name: 'addresslist',
    component: () => import("../views/proilte/addre/AddresslistView.vue"),
    meta: {
      title: '地址编辑'
    }
  },
  {
    //订单预览
    path: '/createOrder',
    name: 'createOrder',
    component: () => import("../views/order/CreateOrder.vue")
  }


]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
//导航守卫
router.beforeEach((to, from, next) => {
  //如果没哟登录，到login
  if (to.meta.isAuthRequired && store.state.user.isLogin == false) {
    Notify('您还没有登录，请先登录')
    return next('/login')
  } else {
    next();
  }
  //设置标题
  document.title = to.meta.title;


})
export default router
